//
//  ViewController.m
//  ProjectA
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ModelForScrollView.h"  //M
#import "ModelForClickSearch1.h"
#import "ModelForClickSearch2.h"
#import "ModelForSection1.h"
#import "ModelForSection2.h"
/**********************************/
#import "ViewForHeader.h"
#import "ViewForSection1Header.h"
#import "CellTableViewSection1.h"
#import "ViewForSection2Header.h"
#import "CellTableViewSection2.h"
#import "CellForClickSearch.h"
#import "ViewForCollectionHeaderForSearch.h"
/***********************************/
#import "ViewController.h"  // C
#import "ViewControllerForClickButton.h"
#import "ControllerForClickItem.h"
#import "ControllerForClickCollectionView.h"
#import "ViewControllerForSearch.h"
#import "ViewControllerForDestination.h"
#import "ViewControllerForTravel.h"
/***********************************/



/***********************************/

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate>


@property (nonatomic, strong) ViewForHeader *viewForHeader;
@property (nonatomic, strong) NSMutableArray *arrForScollView;  //Model轮播图数据
@property (nonatomic, strong) NSMutableArray *arrForClickSearch1;
@property (nonatomic, strong) NSMutableArray *arrForClickSearch2;
@property (nonatomic, strong) NSMutableArray *arrForSection2;
@property (nonatomic, strong) NSMutableArray *arrForSearchData;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSNumber *number;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *button;

@end

@implementation ViewController
- (void)dealloc {
    
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_viewForHeader release];
    [_button release];
    [_textField release];
    [_number release];
    [_collectionView release];
    [_tableView release];
    [_arrForSearchData release];
    [_arrForSection2 release];
    [_arrForClickSearch2  release];
    [_arrForClickSearch1 release];
    [_arrForScollView release];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Data" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"webViewURL" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"clickButton" object:nil];
    
    [super dealloc];
    
}
- (void)setNightDayModel {
    [super setNightDayModel];
    self.collectionView.backgroundColor = RGBA(52, 52, 52, 1);;
  
    [self.button setTintColor:L_COLOR_WHITE];

    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);;
}
- (void)setLightDayModel {
    [super setLightDayModel];

    self.collectionView.backgroundColor = RGBA(255, 251, 222, 1);
    
    [self.button setTintColor:L_COLOR_BLACK];

    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
   
}

- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
    //  搜索界面将要出现时, 刷新界面并且刷新数据
    // [self.collectionView reloadData];
    [self handleForSearchData];
    
    [UIView setColorWithButton:self.button nightColor:L_COLOR_WHITE dayColor:L_COLOR_BLACK];
    [UIView setColorWithTextField:self.textField nightColor:L_COLOR_LIGHTGRAY dayColorWithRed:68 green:177 blue:207 alpha:1];
    
    // 开始轮播图自动播放
    [self.viewForHeader createTimer];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
    // 停止轮播图的自动播放
    [self.viewForHeader invalTimer];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     
    // 加载数据菊花
     [self.view addSubview:[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame]];
    
    [self handleData];
    
    [self handleForSearchData];
    
    [self settingsNavigationController];
    
    [self createTableView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 首页面的轮播图推出
    [[NSNotificationCenter defaultCenter] addObserverForName:@"webViewURL" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        NSString *url = [note.userInfo objectForKey:@"url"];
        ControllerForClickCollectionView *controller = [[ControllerForClickCollectionView alloc] init];
        controller.url = url;
        [controller setHidesBottomBarWhenPushed:YES];   //隐藏将要显示vc的tabBar
        self.tabBarController.tabBar.hidden = YES;
        self.navigationController.navigationBar.hidden = NO;
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
        
    }];
    
    
    // 点击全部button界面推出
    [[NSNotificationCenter defaultCenter] addObserverForName:@"clickButton" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        ViewControllerForClickButton *clickButton = [[ViewControllerForClickButton alloc] init];
        clickButton.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:clickButton animated:YES];
        [clickButton release];
        self.tabBarController.tabBar.hidden = YES;
        self.navigationController.navigationBar.hidden = NO;
    }];
    
    // 点击item的界面推出
    [[NSNotificationCenter defaultCenter] addObserverForName:@"Data" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        ModelForSection1 *model = [note.userInfo objectForKey:@"data"];
        NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@", model.spot_id];
        NSLog(@"%@", url);
        ControllerForClickItem *item = [[ControllerForClickItem alloc] init];
        item.spot_id = url;
        item.model = model;
        
        self.tabBarController.tabBar.hidden = YES;

        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:item animated:YES];
        [item release];
        self.hidesBottomBarWhenPushed = NO;
    }];
}


- (void)settingsNavigationController {

    
    
    self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button.frame = CGRectMake(0, 0, 30, 25);
    
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitle:@"附近" forState:UIControlStateNormal];
    
    UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithCustomView:self.button] autorelease];
    self.navigationItem.rightBarButtonItem = barButton;
    
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 80, 25)];
    self.textField.layer.masksToBounds = YES;
    self.textField.layer.cornerRadius = self.textField.frame.size.height / 2.f;
    self.textField.placeholder = @"搜索目的地、游记、故事集、用户";
    self.textField.font = [UIFont systemFontOfSize:13];
    self.textField.textAlignment = 1;
    self.textField.delegate = self;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField.returnKeyType = UIReturnKeySearch;
    self.textField.center = self.navigationItem.titleView.center;
    self.navigationItem.titleView = self.textField;
    [self.textField release];

}

// 将要开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    self.tabBarController.tabBar.hidden = YES;
    
    if (!self.collectionView) {
        
        [self handleDataForClickSearch];
        [self createCollectionView];
        
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.collectionView reloadData];
        [self handleForSearchData];
        self.collectionView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.bounds.size.height - 64);
        [self.button setTitle:@"取消" forState:UIControlStateNormal];
        
    }];
    return YES;
}

// 点击键盘的确认键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    // 判断当前输入框是否为空
    if (![textField.text  isEqual: @""]) {  //不为空时
        
        // 首先判断是否搜索过该关键字
        NSArray *arr = [[NSUserDefaults standardUserDefaults] arrayForKey:@"搜索历史"];
        NSMutableArray *arrForSearchData = [NSMutableArray arrayWithArray:arr];
        
        if ([arr containsObject:textField.text]) {
            [arrForSearchData removeObject:textField.text];
            [arrForSearchData addObject:textField.text];
            [[NSUserDefaults standardUserDefaults] setObject:arrForSearchData forKey:@"搜索历史"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        } else {
            
            [arrForSearchData addObject:textField.text];
            [[NSUserDefaults standardUserDefaults] setObject:arrForSearchData forKey:@"搜索历史"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }
        
        ViewControllerForSearch *ctrSearch = [[ViewControllerForSearch alloc] init];
        ctrSearch.search = textField.text;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:ctrSearch animated:YES];
        [ctrSearch release];
        self.hidesBottomBarWhenPushed = NO;
        
    }
    return YES;
}

- (void)buttonAction:(UIButton *)button {
    
    if ([button.currentTitle isEqualToString:@"取消"]) {
        [self.textField resignFirstResponder];
        self.tabBarController.tabBar.hidden = NO;
            [UIView animateWithDuration:0.25 animations:^{
                self.collectionView.frame = CGRectMake(0, 64, self.view.frame.size.width, 0);
                [self.button setTitle:@"附近" forState:UIControlStateNormal];
                self.textField.text = NULL;
            }];
    }
}


- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.headerReferenceSize = CGSizeMake(0, 50);
   
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 0) collectionViewLayout:flowLayout];
    [flowLayout release];
    
    if (ISNIGHT) {
        self.collectionView.backgroundColor = RGBA(52, 52, 52, 1);
    } else {
        
        self.collectionView.backgroundColor = RGBA(255, 251, 222, 1);
    }
    
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CellForClickSearch class] forCellWithReuseIdentifier:@"poolForClickSearch"];
    [self.collectionView registerClass:[ViewForCollectionHeaderForSearch class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForCollectionViewHeader"];
    
    
    self.collectionView.alwaysBounceVertical = YES;
}



- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];

    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self handleData];
     
        });

        
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/index/?next_start=%@", self.number];
            [NetWorkHandler networkingWithUrl:str comleteHandle:^(id result , NSData *data) {
                NSDictionary *dicData = [result objectForKey:@"data"];
                NSArray *arrElements = [dicData objectForKey:@"elements"];
                self.number = [dicData objectForKey:@"next_start"];
                for (NSDictionary *dic in arrElements) {
                    
                    NSArray *arr4 = [dic objectForKey:@"data"];
                    ModelForSection2 *model4 = [[ModelForSection2 alloc] init];
                    [model4 setValuesForKeysWithDictionary:[arr4 lastObject]];
                    [self.arrForSection2 addObject:model4];
                    [model4 release];
                    
                }
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            }];
            
        });
        
    }];
    
    // 注册第一区的头部
    [self.tableView registerClass:[ViewForSection1Header class] forHeaderFooterViewReuseIdentifier:@"poolForSection1Header"];
    // 注册第一区的cell(四个item)
    [self.tableView registerClass:[CellTableViewSection1 class] forCellReuseIdentifier:@"cellTableViewSection1"];
    
    // 注册第二区的头部
    [self.tableView registerClass:[ViewForSection2Header class] forHeaderFooterViewReuseIdentifier:@"poolForSection2Header"];

    // 注册第二区的cell
    [self.tableView registerClass:[CellTableViewSection2 class] forCellReuseIdentifier:@"cellTableViewSection2"];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (0 == section)      {return self.arrForSearchData.count;}
    else if (1 == section) {return self.arrForClickSearch1.count;}
    else                   {return self.arrForClickSearch2.count;}

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForClickSearch *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForClickSearch" forIndexPath:indexPath];
    if (0 == indexPath.section) {
        
        NSString *str = [self.arrForSearchData objectAtIndex:indexPath.item];
        [cell passStr:str];
        return cell;
        
    } else if (1 == indexPath.section) {
        
        [cell passModel1:[self.arrForClickSearch1 objectAtIndex:indexPath.item]];
        return cell;
        
    } else {
        
        [cell passModel1:[self.arrForClickSearch2 objectAtIndex:indexPath.item]];
    }
    return cell;
}


#pragma mark - UICollectionViewDelegate

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    ViewForCollectionHeaderForSearch *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForCollectionViewHeader" forIndexPath:indexPath];
    switch (indexPath.section) {
        case 0: {
            view.label.text = @"搜索历史";
            view.button.hidden = NO;
            [view.button addTarget:self action:@selector(clearAllForData:) forControlEvents:UIControlEventTouchUpInside];
            return view;
            break;
        }
        case 1: {
            view.label.text = @"国外热门目的地";
           
            view.button.hidden = YES;
            return view;
        }
        case 2: {
            view.label.text = @"国内热门目的地";
           
            view.button.hidden = YES;
            return view;
        }
        default: {
            return nil;
            break;
        }
    }
    
}
// 点击清除全部历史记录的方法
#pragma mark ** 清除全部历史记录方法
- (void)clearAllForData:(UIButton *)button {
    NSLog(@"清除全部历史记录");
    NSArray *arr = [NSArray array];
    [[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"搜索历史"];
    [self.collectionView reloadData];
    [self.arrForSearchData removeAllObjects];
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGSize size;
    if (0 == indexPath.section) {
        
        NSString *name = [self.arrForSearchData objectAtIndex:indexPath.item];
      
        size = [name sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
        
    } else if (1 == indexPath.section) {
        ModelForClickSearch1 *model = [self.arrForClickSearch1 objectAtIndex:indexPath.item];
        size = [model.name sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
        
    } else {
        ModelForClickSearch2 *model = [self.arrForClickSearch2 objectAtIndex:indexPath.item];
        size = [model.name sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    }
    CGSize result = CGSizeMake(size.width + 30, size.height + 20);
    if (result.width >= self.view.bounds.size.width - 30) {
        result.width = self.view.bounds.size.width - 30;
        return result;
    } else {
        
    }
    return result;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (1 == indexPath.section) {
        //http://api.breadtrip.com//destination/place/3/9990/photos/?gallery_mode=1&count=18&start=0
        ModelForClickSearch1 *model = [self.arrForClickSearch1 objectAtIndex:indexPath.item];
        NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/", model.type, model.id];
        ViewControllerForDestination *destination = [[ViewControllerForDestination alloc] init];
        destination.titleForBar = model.name;
        destination.url = url;
        self.tabBarController.tabBar.hidden = YES;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:destination animated:NO];
        self.hidesBottomBarWhenPushed = NO;
        [destination release];
    }
    if (2 == indexPath.section) {
        ModelForClickSearch2 *model = [self.arrForClickSearch2 objectAtIndex:indexPath.item];
        NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/", model.type, model.id];
        ViewControllerForDestination *destination = [[ViewControllerForDestination alloc] init];
        destination.titleForBar = model.name;
        destination.url = url;
        self.tabBarController.tabBar.hidden = YES;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:destination animated:NO];
        self.hidesBottomBarWhenPushed = NO;
        [destination release];

    }
    
}


#pragma mark - UITableViewDelegate
// 选中row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ViewControllerForTravel *travel = [[ViewControllerForTravel alloc] init];
    ModelForSection2 *model = [self.arrForSection2 objectAtIndex:indexPath.row];
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/?gallery_mode=1&sign=e27ff5dd6cefbda2efc343868822d433", model.id];
    NSLog(@"%@", url);
    travel.url = url;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:travel animated:YES];
    [travel release];
    self.hidesBottomBarWhenPushed = NO;
}

// 去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
// 返回tableView分区头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (0 == section) {
        ViewForSection1Header *view1 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForSection1Header"];
        
       
        [UIView setColorWithView:view1.contentView nightColor:RGBA(52, 52, 52, 1) dayColorWithRed:255 green:251 blue:222 alpha:1];
        return view1;
    } else {    //区头是2
        ViewForSection2Header *view2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForSection2Header"];
        
        
        [UIView setColorWithView:view2.contentView nightColor:RGBA(52, 52, 52, 1) dayColorWithRed:255 green:251 blue:222 alpha:1];
        return view2;
    }
}

// 返回分区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 40;
}

#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section) {
        // 四个item的cell
        CellTableViewSection1 *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cellTableViewSection1"];
        cell1.backgroundColor = [UIColor clearColor];
        return cell1;
    } else {
        // 长条游记
        CellTableViewSection2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cellTableViewSection2"];
        cell2.backgroundColor = [UIColor clearColor];
        [cell2 passModel:[self.arrForSection2 objectAtIndex:indexPath.row]];
        return cell2;
    }
}

// 返回分区row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (0 == section) return 1;
 
    return self.arrForSection2.count;
    
}

// 返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section) return 500;
                           else return 200;
    
}

#pragma mark ** 解析数据
- (void)handleData {
	
    [NetWorkHandler networkingWithUrl:@"http://api.breadtrip.com/v2/index/" comleteHandle:^(id result , NSData *data) {
        
        /****************初始化各种装model数组***********/
        self.arrForScollView = [NSMutableArray array];
      //  self.arrForSection2For1 = [NSMutableArray array];
        self.arrForSection2 = [NSMutableArray array];
        self.arrForSearchData = [NSMutableArray array];
        
        NSDictionary *dicData = [result objectForKey:@"data"];
        self.number = [dicData objectForKey:@"next_start"];
        NSArray *arrElements = [dicData objectForKey:@"elements"];
        /****************轮播图数据部分*****************/
        NSMutableArray *arrTemp1 = [NSMutableArray array];
        for (NSDictionary *dic in arrElements) {
            NSNumber *number1 = [dic objectForKey:@"type"];
            if ([number1 isEqualToNumber:@(1)]) {
                [arrTemp1 addObject:dic];
            }
        }
        for (NSDictionary *dic in arrTemp1) {
            NSArray *arr1 = [dic objectForKey:@"data"];
            NSArray *arrTemp1 = [arr1 lastObject];
            for (NSDictionary *dic in arrTemp1) {
                ModelForScrollView *model1 = [[ModelForScrollView alloc] init];
                [model1 setValuesForKeysWithDictionary:dic];
                [self.arrForScollView addObject:model1];
                [model1 release];
            }
        }
        
        self.viewForHeader = [[[ViewForHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)] autorelease];
        self.tableView.tableHeaderView = self.viewForHeader;

        self.viewForHeader.arrForModel = self.arrForScollView;
 
        /*************Section2数据部分***************/
        NSMutableArray *arrTemp4 = [NSMutableArray array];
        for (NSDictionary *dic in arrElements) {
            NSNumber *number4 = [dic objectForKey:@"type"];
            if ([number4 isEqualToNumber:@(4)]) {
                [arrTemp4 addObject:dic];
            }
        }
        for (NSDictionary *dic in arrTemp4) {
            NSArray *arr4 = [dic objectForKey:@"data"];
            ModelForSection2 *model4 = [[ModelForSection2 alloc] init];
            [model4 setValuesForKeysWithDictionary:[arr4 lastObject]];
            [self.arrForSection2 addObject:model4];
            [model4 release];
        }
        
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
        // 结束加载数据菊花
        [[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame] didFinishLoad];
        
    }];
    
}

- (void)handleDataForClickSearch {
    self.arrForClickSearch1 = [NSMutableArray array];
    self.arrForClickSearch2 = [NSMutableArray array];
    [NetWorkHandler networkingWithUrl:@"http://api.breadtrip.com/v2/index/" comleteHandle:^(id result, NSData *data) {
        
        NSDictionary *dicForData = [result objectForKey:@"data"];
        NSArray *arrForSearch_data = [dicForData objectForKey:@"search_data"];
        NSDictionary *dic1 = [arrForSearch_data firstObject];
        NSDictionary *dic2 = [arrForSearch_data lastObject];
        NSArray *arrForElements1 = [dic1 objectForKey:@"elements"];
        NSArray *arrForElements2 = [dic2 objectForKey:@"elements"];
        for (NSDictionary *dic in arrForElements1) {
            ModelForClickSearch1 *model = [[ModelForClickSearch1 alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForClickSearch1 addObject:model];
            [model release];
        }
        for (NSDictionary *dic in arrForElements2) {
            
            ModelForClickSearch2 *model = [[ModelForClickSearch2 alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForClickSearch2 addObject:model];
            [model release];
            
        }
        
        [self.collectionView reloadData];
    }];
}

- (void)handleForSearchData {
    
    self.arrForSearchData = [NSMutableArray array];
    NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"搜索历史"];

    for (NSString *data in arr) {
        [self.arrForSearchData insertObject:data atIndex:0];
    }
        [self.collectionView reloadData];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
